﻿using Aspose.Cells;
using MediaInfoNET;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

namespace GetMp4Len
{
    /// <summary>
    /// https://www.cnblogs.com/jiangxianshen/p/7561265.html
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Text = Directory.GetCurrentDirectory();
            this.Text = "视频长度获取";
            this.StartPosition = FormStartPosition.CenterScreen; 
            //Test(@"C:\Users\wlx\Videos\2018-09-11_14-50-53.mp4");
        }

        private void Test(String Path)
        {
            Console.WriteLine("视频信息测试：");
            //文件路径
            string videoFilePath = Path;
            //实例化
            MediaFile videoFile = new MediaFile(videoFilePath);

            Console.WriteLine("基本信息:");
            Console.WriteLine("完整名称：{0}", videoFile.File);
            Console.WriteLine("文件名称：{0}", videoFile.Name);
            Console.WriteLine("格式: {0}", videoFile.General.Format);
            Console.WriteLine("视频时长 : {0}", videoFile.General.DurationString);
            Console.WriteLine("视频码率(比特率) : {0}", videoFile.General.Bitrate);
            Console.WriteLine("概述: {0}", videoFile.Description);
            Console.WriteLine();
            if (videoFile.Video.Count > 0)
            {
                Console.WriteLine("视频信息：");
                Console.WriteLine("格式: {0}", videoFile.Video[0].Format);
                Console.WriteLine("码率: {0}", videoFile.Video[0].Bitrate.ToString());
                Console.WriteLine("分辨率: {0}", videoFile.Video[0].FrameSize.ToString());
                Console.WriteLine("视频概述: {0}", videoFile.Video[0].Description);
            }
            Console.WriteLine();
            if (videoFile.Audio.Count > 0)
            {
                Console.WriteLine("音频信息：");
                Console.WriteLine("格式 : {0}", videoFile.Audio[0].Format);
                Console.WriteLine("码率: {0}", videoFile.Audio[0].Bitrate.ToString());
                Console.WriteLine("声道 : {0}", videoFile.Audio[0].Channels.ToString());
                Console.WriteLine("采样率: {0}", videoFile.Audio[0].SamplingRate.ToString());
                Console.WriteLine("音频概述: {0}", videoFile.Audio[0].Description);
            }
            Console.WriteLine();
        }

        private String[] GetVideoInfo(String videoFilePath)
        {
            MediaFile videoFile = new MediaFile(videoFilePath);
            return new string[] { videoFile.Name, videoFile.General.DurationString };
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            Workbook wb = new Workbook();
            Worksheet ws = wb.Worksheets[0];
            Cells cell = ws.Cells;
            cell[0, 0].PutValue("文件名称");//添加数据到第0行和第0列
            cell[0, 1].PutValue("视频时长");
            cell[0, 2].PutValue("分钟");
            cell.SetColumnWidth(0, 50); //设置列宽

            //String path = @"C:\Users\wlx\Videos\";
            String path = this.textBox1.Text;
            //第一种方法
            string[] files = Directory.GetFiles(path);
            int index = 1;
            Array.Sort(files);
            foreach (string file in files)
            {

                String[] row = GetVideoInfo(file);
                cell[index, 0].PutValue(row[0]);
                cell[index, 1].PutValue(row[1]);
                DateTime dt = DateTime.ParseExact(row[1], "hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                cell[index,2].PutValue(dt.Hour * 60 + dt.Minute);
                index++;

            }
            wb.Save(this.textBox1.Text + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
            MessageBox.Show("生成成功!");
        }
    }


}
